package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class UserDao extends DAO{
    private Connection conn = null;
    private PreparedStatement pst = null;
    ResultSet rs = null;
    ResultSetMetaData rd = null;
    public boolean insert(String userName,String password) {
        boolean flag = false;
        try {
            conn = this.getConnection();
            pst = conn.prepareStatement("insert into tb_user (uName,uPassword) values(?,?)");
            pst.setString(1, userName);
            pst.setString(2,password);
            flag = pst.execute();
            pst.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("用户注册插入出错，"+e.getMessage());
        }
        return flag;
    }

    public String queryByName(String userName) {
        String result = null;
        try {
            conn = this.getConnection();
            pst = conn.prepareStatement("select uPassword from tb_user where uName=?");
            pst.setString(1, userName);
            rs = pst.executeQuery();
            rs.last();
            if(rs.getRow()>0)
                result = rs.getString(1);//查询成功
            rs.close();
            pst.close();
            conn.close();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("用户查询出错，"+e.getMessage());
        }
        return result;
    }

}
